<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\validate\Admin;

class Login
{
    //
    public function login()
    {
        if (request()->isPost()) {
            $params = \request()->only(['username', 'password']);
            $validate = (new Admin)->scene('login');
            if (!$validate->check($params)) {
                result(null, 0, $validate->getError());
            }
            $adminInfo = \app\model\Admin::getByUsername($params['username']);
            if (!$adminInfo) {
                result(null, 0, '用户名不存在');
            }
            if (!password_verify($params['password'], $adminInfo['password'])) {
                result(null, 0, '密码不正确');
            }
            session('adminId', $adminInfo['id']);
            result(null, 1, '登录成功', (string)url('home/wdlFrame'));
        }
        return view();
    }

    // 退出登录
    public function logout()
    {
        session(null);
        result(null, 1, '注销成功', (string)url('login/login', [], 'html'));
    }
}
